/*
 * @Author: zhengzhuang
 * @Date: 2020-05-16 15:28:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-09 10:53:17
 * @Description: App定位权限类
 * @FilePath: /C20Driver_1.0.0/lib/utils/permission_handler.dart
 */
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionHandlerX {
  /// 动态申请位置权限
  static Future<bool> position(context) async {
    // return true;
    // 判断设备是苹果还是安卓：
    if (Platform.isIOS) {
      await PermissionHandler().requestPermissions([PermissionGroup.location]);
    }
    // 只有当用户同时点选了拒绝开启权限和不再提醒后才会true
    bool isSHow = await PermissionHandler()
        .shouldShowRequestPermissionRationale(PermissionGroup.location);
    // 申请结果 权限检测
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location);

    if (permission != PermissionStatus.granted) {
      // 权限没允许
      // 如果弹框不在出现了，那就跳转到设置页。
      // 如果弹框还能出现，那就不用管了，申请权限就行了
      if (!isSHow) {
        _openAlertDialog(context);
        return false;
      } else {
        await PermissionHandler()
            .requestPermissions([PermissionGroup.location]);
        //此时要在检测一遍
        //没允许就就提示。
        PermissionStatus pp = await PermissionHandler()
            .checkPermissionStatus(PermissionGroup.location);
        if (pp == PermissionStatus.granted) {
          //权限允许了
          return true;
        } else {
          // 权限未通过
          _openAlertDialog(context);
          return false;
        }
      }
    } else {
      //权限允许了
      return true;
    }
  }

  static Future<bool> denied(context) async {
    Map<PermissionGroup, PermissionStatus> statuses =
        await PermissionHandler().requestPermissions([
      PermissionGroup.photos,
      PermissionGroup.storage,
    ]);

    final info = statuses[PermissionGroup.storage].toString();
    final infoPhotos = statuses[PermissionGroup.photos].toString();
    print(info);
    print(infoPhotos);
  }
}

enum Action { Ok, Cancel }
Future _openAlertDialog(BuildContext context) async {
  showDialog(
    context: context,
    barrierDismissible: false, //// user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('位置权限未打开'),
        content: Text('请前往打开'),
        actions: <Widget>[
          FlatButton(
            child: Text('取消'),
            onPressed: () async {
              if (Platform.isAndroid) {
                Navigator.pop(context);
              } else {
                Navigator.pop(context);
              }
            },
          ),
          FlatButton(
            child: Text('确认'),
            onPressed: () {
              Navigator.pop(context);
              openSetting(context);
            },
          ),
        ],
      );
    },
  );
}

Future openSetting(BuildContext context) async {
  bool isOpened = await PermissionHandler().openAppSettings();
  if (isOpened) {
    return '打开了设置页！';
  } else {
    return '没打开！';
  }
}
